package com.postgresql.geoserver;

import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import it.geosolutions.geoserver.rest.HTTPUtils;
import it.geosolutions.geoserver.rest.decoder.RESTDataStore;
import it.geosolutions.geoserver.rest.decoder.RESTLayer;
import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder;
import it.geosolutions.geoserver.rest.encoder.datastore.GSPostGISDatastoreEncoder;
import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * @author: 顾志杰
 */
public class Temp {
    //GeoServer的连接配置
   private static String url = "http://localhost:8080/geoserver" ;
    private static String username = "admin" ;
    private static  String passwd = "geoserver" ;

    //postgis连接配置
    private static String postgisHost = "192.168.24.203" ;
    private static  int postgisPort = 5432 ;
    private static String postgisUser = "postgres" ;
    private static String postgisPassword = "postgres" ;
    private static String postgisDatabase = "gis" ;
    //待创建和发布图层的工作区名称workspace
    private static String ws = "ws" ;
    //待创建和发布图层的数据存储名称store
    private static String store_name = "grid03" ;
    // 数据库要发布的表名称,后面图层名称和表名保持一致
    private static String table_name = "grid03" ;



    public static void main(String[] args) throws MalformedURLException {
        URL u = new URL(url);
        GeoServerRESTManagerEx manager = new GeoServerRESTManagerEx(u, username, passwd);

        //判断工作区（workspace）是否存在，不存在则创建
//        workspace(manager);

        //判断数据存储（datastore）是否已经存在，不存在则创建
//        datastore(manager);

        //判断图层是否已经存在，不存在则创建并发布
//        layer(manager);

        String urlStr = url + "/rest/layers/ws:grid03/styles";
        String styleSts = "<StyleInfoPost>\n" +
                "\t<name>restricted</name>\n" +
                "\t<filename>restricted.sld</filename>\n" +
                "</StyleInfoPost>";

        String s = HTTPUtils.postXml(urlStr, styleSts, username, passwd);
        System.out.println(s);
        String s1 = HTTPUtils.get(url + "/rest/layergroups",username,passwd);
        System.out.println(s1);
    }


    public static void workspace(GeoServerRESTManagerEx manager){
        GeoServerRESTPublisher publisher = manager.getPublisher() ;
        List<String> workspaces = manager.getReader().getWorkspaceNames();
        if(!workspaces.contains(ws)){
            boolean createws = publisher.createWorkspace(ws);
            System.out.println("create ws : " + createws);
        }else {
            System.out.println("workspace已经存在了,ws :" + ws);
        }
    }


    public static void datastore(GeoServerRESTManagerEx manager){
        RESTDataStore restStore = manager.getReader().getDatastore(ws, store_name);
        if(restStore == null){
            GSPostGISDatastoreEncoder store = new GSPostGISDatastoreEncoder(store_name);
            //设置url
            store.setHost(postgisHost);
            //设置端口
            store.setPort(postgisPort);
            // 数据库的用户名
            store.setUser(postgisUser);
            // 数据库的密码
            store.setPassword(postgisPassword);
            // 那个数据库;
            store.setDatabase(postgisDatabase);
            //当前先默认使用public这个schema
            store.setSchema("public");
            store.setConnectionTimeout(20);
            // 超时设置
            //store.setName(schema);
            // 最大连接数
            store.setMaxConnections(20);
            // 最小连接数
            store.setMinConnections(1);
            store.setExposePrimaryKeys(true);
            boolean createStore = manager.getStoreManager().create(ws, store);
            System.out.println("create store : " + createStore);
        } else {
            System.out.println("数据存储已经存在了,store:" + store_name);
        }
    }


    public static void layer(GeoServerRESTManagerEx manager){
        RESTLayer layer = manager.getReader().getLayer(ws, table_name);
        if(layer == null){
            GSFeatureTypeEncoder pds = new GSFeatureTypeEncoder();
            pds.setTitle(table_name);
            pds.setName(table_name);
            pds.setSRS("EPSG:3857");
            GSLayerEncoder layerEncoder = new GSLayerEncoder();
            String publish = manager.getPublisherEx().publishDBLayerStr(ws, store_name,  pds, layerEncoder);
            System.out.println("Url : " + publish);
        }else {
            System.out.println("表已经发布过了,table:" + table_name);
        }
    }
}
